package com.cyru.statemachine.core.impl;


import com.cyru.statemachine.Visitor;
import com.cyru.statemachine.core.State;
import com.cyru.statemachine.core.StateMachine;
import com.cyru.statemachine.core.Transition;

/**
 * Created on 2021/8/3.
 *
 * @author zhang ling
 */
public class SysOutVisitor implements Visitor {
    @Override
    public String visitOnEntry(StateMachine<?, ?, ?> stateMachine) {
        return "------------"+stateMachine.getId()+"--------------";
    }

    @Override
    public String visitOnExsit(StateMachine<?, ?, ?> stateMachine) {
        return "------------------------------";
    }

    @Override
    public String visitState(State<?, ?, ?> state) {
        StringBuilder sb = new StringBuilder();
        for (Transition<?, ?, ?> transition : state.getAllTransition()) {
            sb.append(transition.getSourceState().getId())
                    .append(" --> ")
                    .append(transition.getTargetState().getId())
                    .append(" : ").append(transition.getTrigger().getEvent())
                    .append(" when ").append(transition.getTrigger().getCondition().getClass().getSimpleName())
                    .append(" then ").append(transition.getTrigger().getAction().getClass().getSimpleName())
                    .append(Visitor.newLine);
        }
        return sb.toString();
    }
}
